package com.fjl.tomcat;

import com.project.WebApplicationConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.nio.charset.Charset;
import java.util.List;

@Configuration
//@ComponentScan(basePackages = {"com.fjl"},includeFilters = {
//      @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {RestController.class, Controller.class})
//},useDefaultFilters =false)
@ComponentScan(basePackageClasses = WebAppConfig.class)
@EnableWebMvc   // ≈<mvc:annotation-driven/>
public class WebAppConfig implements WebMvcConfigurer {
	//设置响应信息编码集
	@Override
	public void extendMessageConverters(
			List<HttpMessageConverter<?>> converters) {
		StringHttpMessageConverter stringHttpMessageConverter =
				(StringHttpMessageConverter) converters.get(1);
		stringHttpMessageConverter.setDefaultCharset(
				Charset.forName("utf-8"));
	}

	//提供静态资源的支持
	@Override
	public void addResourceHandlers(
			ResourceHandlerRegistry registry) {
		//客户端以/html/开始的请求，访问类路径下static/html
		registry.addResourceHandler("/html/**").
				addResourceLocations("classpath:/static/html/");
	}
	/**
	 * 配置拦截器
	 *
	 * @return
	 */
//	@Bean
//	public TulingInterceptor tulingInterceptor() {
//		return new TulingInterceptor();
//	}

	/**
	 * 文件上传下载的组件
	 *
	 * @return
	 */
//	@Bean
//	public MultipartResolver multipartResolver() {
//		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
//		multipartResolver.setDefaultEncoding("UTF-8");
//		multipartResolver.setMaxUploadSize(1024 * 1024 * 10);
//		return multipartResolver;
//	}

	/**
	 * 注册处理国际化资源的组件
	 *
	 * @return
	 */
/* @Bean
   public AcceptHeaderLocaleResolver localeResolver() {
      AcceptHeaderLocaleResolver acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
      return acceptHeaderLocaleResolver;
   }*/
//	@Override
//	public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(tulingInterceptor()).addPathPatterns("/*");
//	}


	/**
	 * 方法实现说明:配置试图解析器
	 *
	 * @author:xsls
	 * @exception:
	 * @date:2019/8/6 16:23
	 */
//	@Bean
//	public InternalResourceViewResolver internalResourceViewResolver() {
//		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
//		viewResolver.setSuffix(".jsp");
//		viewResolver.setPrefix("/WEB-INF/jsp/");
//		return viewResolver;
//	}


//	@Override
//	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//		converters.add(new MappingJackson2HttpMessageConverter());
//	}
}